#include "head.h"

extern char token[512];
static int split(char* src, char** command, char** argument);
static int reNewline(char *str);
static bool is_short_command(char* command);
static bool is_long_command(char* command);
static int short_command_handler(char* command, char* argument, int socket_fd);
static int long_command_handler(char* command, char* argument, 
                                shared_resource_t* pshared_resource);

int stdin_handler(int socket_fd, shared_resource_t* pshared_resource){
    // 处理标准输入
    char buf[1024] = {0};
    read(STDIN_FILENO,buf,sizeof(buf));
    char* command = NULL;
    char* argument = NULL;
    split(buf, &command, &argument);
    reNewline(command);
    reNewline(argument);
    if(command == NULL || (strcmp(command,"") == 0)){
        return 1;
    }
    if(is_short_command(command)){
       return short_command_handler(command, argument, socket_fd);
    }
    else if(is_long_command(command)){
       return long_command_handler(command, argument, pshared_resource);
    }
    else{
        printf("请输入正确的命令\n");
        return 1;
    }

    return 0;


}

int short_command_handler(char* command, char* argument, int socket_fd){
    transmit_t transmit;
    bzero(&transmit,sizeof(transmit));
    strcpy(transmit.flag,"SHORTCOMMAND");
    strcpy(transmit.token,token);
    strcpy(transmit.command,command);
    if(argument == NULL || (strcmp(argument,"") == 0)){
       bzero(transmit.argument,sizeof(transmit.argument));
    }
    else{
       strcpy(transmit.argument,argument);
    }
    send(socket_fd,&transmit,sizeof(transmit),MSG_NOSIGNAL);
    return 0;
}


int long_command_handler(char* command, char* argument, shared_resource_t* pshared_resource){
    if(argument == NULL || (strcmp(argument,"") == 0)){
       printf("请输入正确的文件名\n");
       return 1;
    }

    long_command_t long_command;
    bzero(&long_command, sizeof(long_command));
    strcpy(long_command.command, command);
    strcpy(long_command.argument, argument);
    // 加锁 写队列 唤醒子线程
    pthread_mutex_lock(&(pshared_resource->task_queue_mutex));
    en_task_queue(&(pshared_resource->task_queue), &long_command);
    pthread_cond_signal(&(pshared_resource->task_queue_cond));
    pthread_mutex_unlock(&(pshared_resource->task_queue_mutex));

    return 0;
}

int split(char* src, char** command, char** argument){
    while(*src != 0 && *src == ' '){
        *src = 0;
        src++;       
    }
    if(*src == 0){
        *command = NULL;
        *argument = NULL;
        return 0;
    }
    else{
        *command = src; 
    }

    while((*src != 0) && (*src != ' ')){
        src++;       
    }

    if(*src == 0){
        *argument = NULL;
    }
    else{
        while(*src == ' '){
        *src = 0;
        src++;       
        }
        *argument = src;
    }
    return 0;
    
}

int reNewline(char *str)
{
    if (str == NULL)
    {
        return -1;
    }
    int len = strlen(str);
    if (len > 0 && str[len - 1] == '\n')
    {
        str[len - 1] = '\0';
    }
    return 0;
}

bool is_short_command(char* command){
    if(strcmp(command, "ls") == 0){
        return 1;
    }
    else if(strcmp(command, "cd") == 0)
    {
        return 1;
    }
    else if(strcmp(command, "pwd") == 0)
    {
        return 1;
    }
    else if(strcmp(command, "mkdir") == 0)
    {
        return 1;
    }
    else if(strcmp(command, "rm") == 0)
    {
        return 1;
    }

    return 0;
}

bool is_long_command(char* command){

    if(strcmp(command, "puts") == 0){
        return 1;
    }
    else if(strcmp(command, "gets") == 0)
    {
        return 1;
    }
    return 0;
}
